import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Vector;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;


public class NewEvent implements ActionListener, WindowListener
{

	//Shared
	Calendar curTime = Calendar.getInstance();	//For clock
	SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss MM-dd-yyyy"); //For clock
		
	//Window Specific
	JFrame mainFrame=new JFrame("Create New Event");
	JFrame dad=null;
	Boolean Confirmed=false,Continuing=false, justLoad = false;
	Boolean temp1=false,eventExists=false;
	
	//Variables
	Vector<Course> tmpCourse;
	Event tmpEvent=null;

	//Input
	JButton Enter=new JButton("OK"), Cancel=new JButton("Cancel"),X=new JButton(""),XS=new JButton("");
	JPanel Screen=new ImagePanel(new ImageIcon("img/RemoteApp_D2.gif").getImage());
	JComboBox<String> CourseBox=new JComboBox(), SectionBox=new JComboBox();
	JTextField Title=new JTextField();
	JLabel TitleL=new JLabel("Event Name:"),CourseL=new JLabel("Course:"),SectionL=new JLabel("Section:");
	
	//Icon
	Image imgLogo = Toolkit.getDefaultToolkit().createImage("img/Dlsu.png");
	Image imgDel = Toolkit.getDefaultToolkit().createImage("img/del.png");
	Image imgCancel = Toolkit.getDefaultToolkit().createImage("img/cancel.png");
	Image imgNext = Toolkit.getDefaultToolkit().createImage("img/next.png");
	ImageIcon logo, del, cancel, next;

	public NewEvent(JFrame dad)
	{
		//Variables
		justLoad = false;
		eventExists=false;
		tmpCourse=new Vector<Course>();
		this.dad=dad;
		CourseBox.addItem(" ");
		tmpCourse.add(new Course(" "));
		SectionBox.addItem(" ");
		tmpCourse.get(CourseBox.getSelectedIndex()).Sections.add(" ");

		Init();
	}
	
	public NewEvent(JFrame dad, Event tmp)
	{
		//Varialbes
		this.dad=dad;
		justLoad = false;
		Continuing=true;
		justLoad = true;
		Confirmed = true;
		eventExists=true;
		tmpEvent=tmp;
		tmpCourse=tmp.Courses;
		
		//Setting Contents with Loaded Values
		Title.setText(tmp.EventTitle);
		for(int i=0;i<tmpCourse.size();i++)
			CourseBox.addItem(tmpCourse.get(i).CourseCode);
		
		if(!tmpCourse.isEmpty()){
			for(int i=0;i<tmpCourse.get(0).Sections.size();i++)
				SectionBox.addItem(tmpCourse.get(0).Sections.get(i));
		}
		Init();
	}
	
	public NewEvent(JFrame dad, Event tmp,Boolean c)
	{
		//Varialbes
		this.dad=dad;
		justLoad = false;
		Continuing=c;
		Confirmed = true;
		eventExists=true;
		tmpEvent=tmp;
		tmpCourse=tmp.Courses;
		
		//Setting Contents with Loaded Values
		Title.setText(tmp.EventTitle);
		for(int i=0;i<tmpCourse.size();i++)
			CourseBox.addItem(tmpCourse.get(i).CourseCode);
		
		if(!tmpCourse.isEmpty()){
			for(int i=0;i<tmpCourse.get(0).Sections.size();i++)
				SectionBox.addItem(tmpCourse.get(0).Sections.get(i));
		}
		Init();
	}
	
	
	public void actionPerformed(ActionEvent arg0)
	{
		Object source = arg0.getSource();
		if (source == Enter)
		{
			Confirmed=true;
			if(Title.getText().equals(""))
			{
				Confirmed=false;
				JOptionPane.showMessageDialog(null, "ERROR: No Event Name Specified", "ERROR", JOptionPane.ERROR_MESSAGE);
			}
			else if(tmpCourse.isEmpty())
			{
				//Confirmed=false;
				//ERROR MSG HERE : changed, allow no courses
				System.out.print("NO COURSES SPECIFIED...\n");
			}
			else
			{
				for(int i=0;i<tmpCourse.size();i++)
					if(tmpCourse.get(i).Sections.isEmpty())
					{
						Confirmed=false;
						JOptionPane.showMessageDialog(null, "ERROR: Course " + tmpCourse.get(i).CourseCode +" has no section", "ERROR", JOptionPane.ERROR_MESSAGE);
						break;
					}
			}
			if(Confirmed){
				justLoad = false;
				windowClosing(null);
			}
			
		}
		else if (source == Cancel)
		{
			windowClosing(null);
		}
		else if(source == CourseBox)
		{
			switch(arg0.getActionCommand())
			{
			case "comboBoxEdited":
					temp1=true;
					
					for(int i=0;i<CourseBox.getItemCount();i++)
						if(CourseBox.getItemAt(i).equals(CourseBox.getSelectedItem()))
						{
							temp1=false;
							JOptionPane.showMessageDialog(null, "Error: Duplicate Course Code", "ERROR", JOptionPane.ERROR_MESSAGE);
							break;
						}
					if(temp1)
					{
						CourseBox.addItem(CourseBox.getSelectedItem().toString());
						tmpCourse.add(new Course(CourseBox.getSelectedItem().toString()));
					}
					
					SectionBox.removeAllItems();
					System.out.print("\n-Courses-\n");
					for(int i=0;i<tmpCourse.size();i++)
						System.out.print(tmpCourse.get(i).CourseCode+"\n");
					System.out.print("\n---------\n");
				break;
				
			case "comboBoxChanged":
				for(int i=0;i<tmpCourse.size();i++)
					System.out.print(tmpCourse.get(i).CourseCode+"\n");
				System.out.print("---------\n");
				if(CourseBox.getSelectedIndex()>=0)
				{
					SectionBox.removeAllItems();
					for(int i=0;i<tmpCourse.get(CourseBox.getSelectedIndex()).Sections.size();i++)
						SectionBox.addItem(tmpCourse.get(CourseBox.getSelectedIndex()).Sections.get(i));
				}
				break;
			}
		}
		else if(source==SectionBox)
		{
			switch(arg0.getActionCommand())
			{
			case "comboBoxEdited":
				if(CourseBox.getItemCount()>0)
				{
					temp1=true;
					
					for(int i=0;i<SectionBox.getItemCount();i++)
						if(SectionBox.getItemAt(i).equals(SectionBox.getSelectedItem()))
						{
							temp1=false;
							JOptionPane.showMessageDialog(null, "ERROR: Duplicate Section", "ERROR", JOptionPane.ERROR_MESSAGE);
							break;
						}
					if(temp1)
					{
						SectionBox.addItem(SectionBox.getSelectedItem().toString());
						tmpCourse.get(CourseBox.getSelectedIndex()).Sections.add(SectionBox.getSelectedItem().toString());
					}
					
					for(int i=0;i<tmpCourse.size();i++)
					{
						System.out.print(tmpCourse.get(i).CourseCode+"\n");
						for(int j=0;j<tmpCourse.get(i).Sections.size();j++)
							System.out.print("       "+tmpCourse.get(i).Sections.get(j).toString()+"\n");
						System.out.print("     -----\n\n");
					}
					System.out.print("\n---------\n");
					break;
				}
			case "comboBoxChanged":
				for(int i=0;i<tmpCourse.size();i++)
				{
					System.out.print(tmpCourse.get(i).CourseCode+"\n");
					for(int j=0;j<tmpCourse.get(i).Sections.size();j++)
						System.out.print("       "+tmpCourse.get(i).Sections.get(j).toString()+"\n");
					System.out.print("     -----\n\n");
				}
				System.out.print("\n---------\n");
				break;
			}
		}
		else if(source==X)
		{
			if(CourseBox.getItemCount()>0)
			{
					System.out.print("PointedAt "+CourseBox.getSelectedIndex()+"\n");
					System.out.print("Removed "+CourseBox.getSelectedItem().toString()+" - ");
					System.out.print(tmpCourse.get(CourseBox.getSelectedIndex()).CourseCode+"\n");
					tmpCourse.remove(CourseBox.getSelectedIndex());
					CourseBox.removeItemAt(CourseBox.getSelectedIndex());
			}
			else
				JOptionPane.showMessageDialog(null, "ERROR: Can't Delete, No Courses Detected!", "ERROR", JOptionPane.ERROR_MESSAGE);
		}
		else if(source==XS)
		{
			if(SectionBox.getItemCount()>0)
			{
				System.out.print("PointedAt "+SectionBox.getSelectedIndex()+"\n");
				System.out.print("Removed "+SectionBox.getSelectedItem().toString()+" - ");
				System.out.print(tmpCourse.get(CourseBox.getSelectedIndex()).Sections.get(SectionBox.getSelectedIndex()).toString()+"\n");
				tmpCourse.get(CourseBox.getSelectedIndex()).Sections.remove(SectionBox.getSelectedIndex());
				SectionBox.removeItemAt(SectionBox.getSelectedIndex());
			}
			else
				JOptionPane.showMessageDialog(null, "ERROR: Can't delete, No Sections Detected", "ERROR", JOptionPane.ERROR_MESSAGE);
		}
	}

	public void windowActivated(WindowEvent arg0)
	{
		
	}

	public void windowClosed(WindowEvent arg0)
	{
		
	}

	public void windowClosing(WindowEvent arg0)
	{
		System.out.print("Window Closing\n");
		if(Confirmed && !justLoad)
		{
			if(eventExists)
				new EventWindow(dad, tmpEvent);
			else
			{
					new EventWindow(dad, new Event(1,tmpCourse,Title.getText()));
			}
			mainFrame.dispose();
			
		}
		else
		{
			dad.setVisible(true);
			mainFrame.dispose();
			
		}
	}

	public void windowDeactivated(WindowEvent arg0)
	{
		
	}

	public void windowDeiconified(WindowEvent arg0)
	{
		
	}

	public void windowIconified(WindowEvent arg0)
	{
		
	}

	public void windowOpened(WindowEvent arg0)
	{
		
	}
	
	private void Init()
	{
		Image newimg = imgDel.getScaledInstance(20, 20, java.awt.Image.SCALE_SMOOTH) ;  
		del = new ImageIcon( newimg );
		Image newimg2 = imgNext.getScaledInstance(15, 15, java.awt.Image.SCALE_SMOOTH) ;  
		next = new ImageIcon( newimg2 );
		Image newimg3 = imgCancel.getScaledInstance(20, 20, java.awt.Image.SCALE_SMOOTH) ;  
		cancel = new ImageIcon( newimg3 );
		//Window Specifics		
		mainFrame.setIconImage(imgLogo);
		mainFrame.addWindowListener(this);
		mainFrame.setLayout(null);
		mainFrame.setSize(400,220);
		mainFrame.setResizable(false);
		mainFrame.setLocationRelativeTo(null);
		mainFrame.setVisible(true);
		mainFrame.add(Screen);
		mainFrame.setDefaultCloseOperation(mainFrame.DISPOSE_ON_CLOSE);
		
		//Screen Specifics
		Screen.setLayout(null);
		Screen.setBounds(0,0,400,220);	
		
		//JComponents
		Enter.setBounds(Screen.getWidth()/2+80-50,150,100,25);
		Cancel.setBounds(Screen.getWidth()/2-80-50,150,100,25);
		CourseBox.setBounds(20,110,120,25);
		CourseBox.setEditable(true);
		SectionBox.setBounds(210,110,80,25);
		SectionBox.setEditable(true);
		X.setBounds(150,110,45,25);
		XS.setBounds(300,110,45,25);
		Title.setBounds(20,45,345,25);
		TitleL.setBounds(20,15,150,25);
		CourseL.setBounds(20,80,100,25);
		SectionL.setBounds(210,80,100,25);
		
		X.setIcon(del);
		XS.setIcon(del);
		Enter.setIcon(next);
		Cancel.setIcon(cancel);
		
		//Adding to Screen
		Screen.add(Enter);
		if(Confirmed)
			Enter.setText("OK");
		else
			Enter.setText("Create");
		
		if(!Continuing || justLoad)
			Screen.add(Cancel);

		
		Screen.add(CourseBox);
		Screen.add(X);
		Screen.add(SectionBox);
		Screen.add(XS);
		Screen.add(Title);
		Screen.add(TitleL);
		Screen.add(CourseL);
		Screen.add(SectionL);
		
		
		//ActionListeners
		Enter.addActionListener(this);
		Cancel.addActionListener(this);
		CourseBox.addActionListener(this);
		SectionBox.addActionListener(this);
		X.addActionListener(this);
		XS.addActionListener(this);
	}
	
}
